package com.appsindy.common.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import com.appsindy.common.bo.GlobalUser;
import com.appsindy.common.json.TableData;
import com.opensymphony.xwork2.ActionContext;

public abstract class AppsindyBaseAction extends AppsindyBaseActionSuper implements UserAware, ServletRequestAware, ServletResponseAware {
    
	public static final String VERSION = "$Rev: 0 $";
    
    protected static final String JSON_RESULT = "jsonResult";
    
    protected static final String REQUEST_RESULT = "requestResult";
    protected static final String R_RESPONSE_TEXT = "R_RESPONSE_TEXT";
    
    protected static final String SESSION_RESULT = "sessionResult";
    protected static final String S_RESPONSE_TEXT = "S_RESPONSE_TEXT";
    
    protected static final String JASPER_RESULT_PDF		= "jasperResultPdf";
    protected static final String JASPER_RESULT_EXCEL	= "jasperResultExcel";
    
    
    private HttpServletRequest request;
    private HttpServletResponse response;
    
    private Object jsonModel;
    private String dir;
    private int startIndex;
    private String sort;
    private int results;
    
    protected GlobalUser globalUser;
    
    public AppsindyBaseAction() {
    }
    
    public String getTable() {
    	log.info("JSON...." + getDir());
        TableData tData = new TableData();
        int sIndex = getStartIndex();
        if(log.isDebugEnabled()) {
            log.debug("---------------------- getTable method this.getClass --------------" + this.getClass());
        }
        List l = getTableData();
        if(log.isDebugEnabled()) {
            log.debug("Start Index=" + sIndex + " data size=" + l.size());
        }
        List outList = null;
        if (sIndex <= l.size()) {
            if ((sIndex + getResults()) <= l.size()) {
            	log.info("Less");
                //outList = (List<CounterPartyType>)l.subList(sIndex, (sIndex + getResults() -1));
                outList = subList(l, sIndex, (getResults() + sIndex - 1));
                tData.setRecords(outList);
            } else {
                log.info("more");
//                tData.setRecords(l.subList(sIndex, (l.size() - 1)));
                outList = subList(l, sIndex, (l.size() - 1));
                tData.setRecords(outList);
            }
        } else {
        	log.info("more 1");
            tData.setRecords(new ArrayList());
        }
        tData.setTotalRecords(l.size());
        tData.setStartIndex(getStartIndex());
        tData.setRecordsReturned(tData.getRecords().size());
        //tData.setRecords(dataLoader.getCounterPartyTypes().);
        if(log.isTraceEnabled()) {
            log.trace("### getTable() :: tData = " + tData.toString());
        }
        setJsonModel(tData);
        response.setHeader("Cache-Control", "no-cache");
        return JSON_RESULT;
    }
    
    private List subList(List list, int from, int to) {
        List l = new ArrayList();
        for(int i = from; i <= to; i++) {
            l.add(list.get(i));
        }
        return l;
    }
    
    public List getTableData() {
        return null;
    }
    
    public Object getJsonModel() {
        return jsonModel;
    }
    public void setJsonModel(Object jsonModel) {
        this.jsonModel = jsonModel;
    }
    
    public String getDir() {
        return dir;
    }
    public void setDir(String dir) {
        this.dir = dir;
    }
    
    public int getStartIndex() {
        return startIndex;
    }
    public void setStartIndex(int startIndex) {
        this.startIndex = startIndex;
    }
    
    public String getSort() {
        return sort;
    }
    public void setSort(String sort) {
        this.sort = sort;
    }
    
    public int getResults() {
        return results;
    }
    public void setResults(int results) {
        this.results = results;
    }

    public void setGlobalUser(GlobalUser globalUser) {
        this.globalUser = globalUser;
    }
    
    public void setServletRequest(HttpServletRequest httpServletRequest) {
        this.request = httpServletRequest;
    }

    public void setServletResponse(HttpServletResponse httpServletResponse) {
        this.response = httpServletResponse;
    }

    protected Map getSession() {
        return ActionContext.getContext().getSession();
    }

    protected HttpServletRequest getRequest() {
        return request;
    }
    
    protected HttpServletResponse getResponse() {
        return response;
    }
    
    protected Object getContextAttribute(String name){
    	return ServletActionContext.getServletContext().getAttribute(name);
    }
    protected void setContextAttribute(String name, Object value){
    	ServletActionContext.getServletContext().setAttribute(name, value);
    }
    
}
